package org.tokaf.rater;

import org.tokaf.TopKElement;
import org.tokaf.datasearcher.DataSearcher;

/**
 * <p>MultiplicationRater multiplies ratings of attributes, obtaining one
 * rating of TopKElement. </p> <p>Copyright (c) 2006</p>
 * @author Alan Eckhardt
 * @version 1.0
 */
public class MultiplicationRater extends Rater {

	public MultiplicationRater(String fields[]) {
		this.fields = (String[]) fields.clone();
	}

	public double getDerivation(int index, DataSearcher[] data, TopKElement el) {
		double a = -1;
		for (int i = 0; i < el.getLength(); i++) {
			if (i == index)
				continue;
			if (el.getRating(i) != 0) {
				if (a == -1)
					a = el.getRating(i);
				else
					a *= el.getRating(i);
			}
		}
		return a;
	}

	public double getDerivation(int index, DataSearcher[] data, Object[] datas) {
		double a = -1;
		for (int i = 0; i < datas.length; i++) {
			if (i == index)
				continue;
			if (data[i].getNormalizer().Normalize(datas[i]) != 0) {
				if (a == -1)
					a = data[i].getNormalizer().Normalize(datas[i]);
				else
					a *= data[i].getNormalizer().Normalize(datas[i]);
			}
		}
		if (a == -1)
			return 0;
		return a;
	}

	public double getRating(TopKElement el, DataSearcher[] data, double optional) {
		double a = -1;
		for (int i = 0; i < el.getLength(); i++) {
			if (el.isNull(i)) {
				if (a == -1)
					a = optional;
				else
					a *= optional;
			} else {
				if (a == -1)
					a = el.getRating(i);
				else
					a *= el.getRating(i);
			}
		}
		return a;
	}

	public double getRating(TopKElement el, DataSearcher[] data,
			TopKElement optional) {
		double a = -1;
		for (int i = 0; i < el.getLength(); i++) {
			if (el.isNull(i)) {
				if (a == -1)
					a = optional.getRating(i);
				else
					a *= optional.getRating(i);
			} else {
				if (a == -1)
					a = el.getRating(i);
				else
					a *= el.getRating(i);
			}
		}
		return a;
	}

}
